﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Xml;

namespace QDT.Payment.AllInPay.Api
{
    public class TradeRequest : Request
    {
        protected List<DetailRequest> details = new List<DetailRequest>();
        /// <summary>
        /// 初始化交易请求报文
        /// </summary>
        /// <param name="trxCode"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="reqSn"></param>
        /// <param name="level"></param>
        /// <param name="businessCode"></param>
        /// <param name="merchantId"></param>
        internal TradeRequest(string trxCode, string userName, string password, string reqSn, int level, string businessCode, string merchantId)
            : base(trxCode, "03", 2, userName, password, reqSn, "BODY")
        {
            m_elInfo.AppendChild(CreateValueElement("LEVEL", level.ToString()));
            XmlElement sum = m_package.CreateElement("TRANS_SUM");
            if (businessCode == null || !Regex.IsMatch(businessCode, "^.{5,6}$"))
                throw new FormatException("业务代码格式错误，应为：C(5,6)");
            sum.AppendChild(CreateValueElement("BUSINESS_CODE", businessCode));
            if (merchantId == null || !Regex.IsMatch(merchantId, "^.{10,15}$"))
                throw new FormatException("银联网络商户ID格式错误，应为：C(10,15)");
            sum.AppendChild(CreateValueElement("MERCHANT_ID", merchantId));
            sum.AppendChild(m_package.CreateElement("SUBMIT_TIME"));
            sum.AppendChild(m_package.CreateElement("TOTAL_ITEM"));
            sum.AppendChild(m_package.CreateElement("TOTAL_SUM"));
            m_elBody.AppendChild(sum);
            m_elBody.AppendChild(m_package.CreateElement("TRANS_DETAILS"));
        }


        /// <summary>
        /// 添加一条详细交易请求至报文
        /// </summary>
        /// <param name="detail"></param>
        public void AddDetail(DetailRequest detail)
        {
            details.Add(detail);
        }

        internal override string ToXML()
        {
            XmlElement el;
            if ((el = (XmlElement)m_package.SelectSingleNode("/" + InternalConstants.RootNode + "/BODY/TRANS_SUM/TOTAL_ITEM")) != null)
            {
                el.RemoveAll();
                el.AppendChild(m_package.CreateTextNode(details.Count.ToString()));
            }
            if ((el = (XmlElement)m_package.SelectSingleNode("/" + InternalConstants.RootNode + "/BODY/TRANS_SUM/TOTAL_SUM")) != null)
            {
                long sum = 0;
                foreach (DetailRequest d in details)
                    sum += d.Amount;
                el.RemoveAll();
                el.AppendChild(m_package.CreateTextNode(sum.ToString()));
            }
            if ((el = (XmlElement)m_package.SelectSingleNode("/" + InternalConstants.RootNode + "/BODY/TRANS_DETAILS")) != null)
            {
                el.RemoveAll();
                foreach (DetailRequest d in details)
                    el.AppendChild(d.ToNode(m_package));
            }
            return base.ToXML();
        }

        internal override void BeforeSend()
        {
            XmlElement el;
            if ((el = (XmlElement)m_package.SelectSingleNode("/" + InternalConstants.RootNode + "/BODY/TRANS_SUM/SUBMIT_TIME")) != null)
            {
                el.RemoveAll();
                el.AppendChild(m_package.CreateTextNode(DateTime.Now.ToString("yyyyMMddHHmmss")));
            }
            base.BeforeSend();
        }
    }
}
